package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.AwStartPlan;
import com.ruoyi.system.manager.StartPlanManager;
import com.ruoyi.system.mapper.AwStartPlanMapper;
import com.ruoyi.system.service.IAwStartPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * 启动计划Service业务层处理
 *
 * @author anxinr
 * @date 2023-08-11
 */
@Service
public class AwStartPlanServiceImpl extends ServiceImpl<AwStartPlanMapper ,AwStartPlan> implements IAwStartPlanService
{
    @Autowired
    private AwStartPlanMapper awStartPlanMapper;

    @Autowired
    private StartPlanManager planManager ;

    /**
     * 查询启动计划
     *
     * @param id 启动计划主键
     * @return 启动计划
     */
    @Override
    public AwStartPlan selectAwStartPlanById(Long id)
    {
        return awStartPlanMapper.selectAwStartPlanById(id);
    }

    /**
     * 查询启动计划列表
     *
     * @param awStartPlan 启动计划
     * @return 启动计划
     */
    @Override
    public List<AwStartPlan> selectAwStartPlanList(AwStartPlan awStartPlan)
    {
        return awStartPlanMapper.selectAwStartPlanList(awStartPlan);
    }

    /**
     * 新增启动计划
     *
     * @param awStartPlan 启动计划
     * @return 结果
     */
    @Override
    public int insertAwStartPlan(AwStartPlan awStartPlan)
    {
        awStartPlan.setCreateTime(DateUtils.getNowDate());
        awStartPlan.setIsDefault(0);
        return awStartPlanMapper.insert(awStartPlan);
    }

    /**
     * 修改启动计划
     *
     * @param awStartPlan 启动计划
     * @return 结果
     */
    @Override
    public int updateAwStartPlan(AwStartPlan awStartPlan)
    {
        awStartPlan.setUpdateTime(DateUtils.getNowDate());
        return awStartPlanMapper.updateById(awStartPlan);
    }

    /**
     * 批量删除启动计划
     *
     * @param ids 需要删除的启动计划主键
     * @return 结果
     */
    @Override
    public int deleteAwStartPlanByIds(Long[] ids)
    {
        return awStartPlanMapper.deleteAwStartPlanByIds(ids);
    }

    /**
     * 删除启动计划信息
     *
     * @param id 启动计划主键
     * @return 结果
     */
    @Override
    public int deleteAwStartPlanById(Long id)
    {
        return awStartPlanMapper.deleteAwStartPlanById(id);
    }

    @Override
    public void setDefault(Long id) {
        if (Objects.isNull(id)) {
            throw new ServiceException("配置主键不能为空！") ;
        }
        LambdaUpdateWrapper<AwStartPlan> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AwStartPlan::getId ,id) ;
        wrapper.set(AwStartPlan::getIsDefault ,1) ;
        this.update(wrapper) ;
        try {
            planManager.resetConfigFileInfoByStartPlanId(id);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
